class state_mechine:
    def __init__( self, init_state ):
        # Map ( input_symbol, current_state ) --> ( action, next_state )
        self.state_transitions = {}

        self.default_transition = None

        self.input_symbol = None
        self.current_state = init_state

        self.next_state = None
        self.action = None

    def add_transition( self, input_symbol, state, action = None, next_state = None ):
        if next_state == None:
            next_state = state

        self.state_transitions[ ( input_symbol, state ) ] = ( action, next_state )

    def get_transition( self, input_symbol, state ):
        if self.state_transitions.has_key( ( input_symbol, state ) ):
            return self.state_transitions[ ( input_symbol, state ) ]
        elif self.default_transition is not None:
            return self.default_transition
        else:
            print 'get_transition error'

    def process( self, input_symbol ):
        self.input_symbol = input_symbol
        
        ( self.action, self.next_state ) = self.get_transition( self.input_symbol, self.current_state )
        if self.action is not None:
            self.action()
        self.current_state = self.next_state
        self.next_state = None
        
        
class HLPlayerTest( state_mechine ):
    """test HLPlayer base class
    """
    
    def __init__( self ):
        state_mechine.__init__( self, 'original' )
        
        self.add_transition( 'start', 'original', self.start, 'playing' )
        self.add_transition( 'forward', 'original', self.forward, 'pausing' )
        self.add_transition( 'backward', 'original', None, None )
        self.add_transition( 'stop', 'playing', self.stop, 'original' )
        self.add_transition( 'pause', 'playing', self.pause, 'pausing' )
        self.add_transition( 'forward', 'playing', self.forward, 'pausing' )
        self.add_transition( 'backward', 'playing', self.backward, 'pausing' )
        self.add_transition( 'start', 'pausing', self.start, 'playing' )
        self.add_transition( 'stop', 'pausing', self.stop, 'original' )
        self.add_transition( 'forward', 'pausing', self.forward, 'pausing' )
        self.add_transition( 'backward', 'pausing', self.backward, 'pausing' )

    def start( self ):
        print 'start'

    def stop( self ):
        print 'stop'
    
    def pause( self ):
        print 'pause'

    def forward( self ):
        print 'forward'

    def backward( self ):
        print 'backward'


def main():
    test = HLPlayerTest()

    test.process( 'start' )

    test.process( 'forward' )

    test.process( 'backward' )

    test.process( 'stop' )


if __name__ == '__main__':
    main()
